package com.chj.design.sk;

public class Code19_State02 {
	public static class AfterNoonState implements WorkState {
		@Override
		public void handle(Working working) {
			if (working.getClock() < 17) {
				System.out.println("状态一般写代码");
			} else {
				working.setConcurrentState(new EveningState());
				working.writeCode();
			}
		}
	}

	public static class EveningState implements WorkState {
		@Override
		public void handle(Working working) {
			if (working.getClock() < 23) {
				System.out.println("加班疲惫写代码");
			} else {
				working.setConcurrentState(work -> {
					System.out.println("别再写了，程序员回家了，明早再试吧😢，现在都已经" + work.getClock() + "点了。放过他吧");
				});
				working.writeCode();
			}
		}
	}

	public static class MorningState implements WorkState {
		@Override
		public void handle(Working working) {
			if (working.getClock() < 12) {
				System.out.println("精神抖擞写代码");
			} else {
				working.setConcurrentState(new NoonState());
				working.writeCode();
			}
		}
	}

	public static class NoonState implements WorkState {
		@Override
		public void handle(Working working) {
			if (working.getClock() < 13) {
				System.out.println("饿了困了写代码");
			} else {
				working.setConcurrentState(new AfterNoonState());
				working.writeCode();
			}
		}
	}

	public interface WorkState {
		/**
		 * 具体的工作情况
		 *
		 * @param working 工作上下文环境
		 */
		void handle(Working working);
	}

	public static class Working {
		/**
		 * 当前工作状态
		 */
		private WorkState concurrentState;

		/**
		 * 当前时刻
		 */
		private int clock;

		public Working(WorkState concurrentState) {
			this.concurrentState = concurrentState;
		}

		public void writeCode() {
			concurrentState.handle(this);
		}

		public WorkState getConcurrentState() {
			return concurrentState;
		}

		public void setConcurrentState(WorkState concurrentState) {
			this.concurrentState = concurrentState;
		}

		public int getClock() {
			return clock;
		}

		public void setClock(int clock) {
			this.clock = clock;
		}
	}

	public static void main(String[] args) {
		Working working = new Working(new MorningState());
		// 手动模拟不同时刻
		working.setClock(9);
		working.writeCode();

		working.setClock(12);
		working.writeCode();

		working.setClock(15);
		working.writeCode();

		working.setClock(21);
		working.writeCode();

		working.setClock(24);
		working.writeCode();
	}
}
